1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.webmacro.util;
23
24 import org.webmacro.Broker;
25 import org.webmacro.WebMacroException;
26
27 import java.lang.reflect.Constructor;
28
29 /***
30 * The LogTargetFactory assists the Broker (and you, if you want) in creating
31 * new LogTarget instances.<p>
32 *
33 * If your LogTarget needs configuration settings from WebMacro, create a
34 * constructor with this signature:<pre>
35 *
36 * public MyLogTarget (org.webmacro.util.Settings settings);
37 *
38 * </pre>
39 *
40 * If you don't need to configuration options, you should have a
41 * null constructor.
42 *
43 * @author e_ridge
44 * @since 0.99
45 */
46 public class LogTargetFactory
47 {
48
49 private static LogTargetFactory _instance = new LogTargetFactory();
50
51 public static class LogCreationException extends WebMacroException
52 {
53
54 /***
55 *
56 */
57 private static final long serialVersionUID = 1L;
58
59 public LogCreationException (String message, Throwable throwable)
60 {
61 super(message, throwable);
62 }
63 }
64
65 /*** Creates new LogTargetFactory */
66 private LogTargetFactory ()
67 {
68 }
69
70 /*** return the only instance of this LogTargetFactory */
71 public static final LogTargetFactory getInstance ()
72 {
73 return _instance;
74 }
75
76 /***
77 * Creates a new <code>org.webmacro.util.LogTarget</code>
78 *
79 * @param broker the Broker that is requesting to create the log. The
80 * Broker is used to find the LogTarget class via the Broker's
81 * <code>.classForName()</code> method.
82 * @param classname the fully-qualified classname of the LogTarget to create
83 * @param settings WebMacro settings that will be passed off to the
84 * new LogTarget during its construction
85 */
86 public final LogTarget createLogTarget (Broker broker, String classname, Settings settings) throws LogCreationException
87 {
88 LogTarget lt = null;
89 try
90 {
91 Class targetClass = broker.classForName(classname);
92 Class[] args = new Class[]{Settings.class};
93 try
94 {
95
96 Constructor constr = targetClass.getConstructor(args);
97 lt = (LogTarget) constr.newInstance(new Object[]{settings});
98 }
99 catch (NoSuchMethodException nsme)
100 {
101
102 lt = (LogTarget) targetClass.newInstance();
103 }
104 }
105 catch (Exception e)
106 {
107 throw new LogCreationException("Cannot create LogTarget "
108 + classname, e);
109 }
110
111 return lt;
112 }
113 }